*===============================================================================
* Project title		Savings Accounts to Borrow Less	
* PIs			Felipe Kast, Dina Pomeranz 
* File name		3a_tables_ancova_borrowing.do
*-------------------------------------------------------------------------------
* Description		Creates borrowing ANCOVA table 
* Outputs		Table A5 
* Spaces per tab 	8 
*===============================================================================

*===============================================================================
*TOC
*1)	Prepare data for analysis
*2)	Tables
*2.1)	Borrowing table short term
*===============================================================================

*===============================================================================
* Section 1 - Prepare data for analysis
*===============================================================================
clear all
global path "~/file_server/project_savings/2_shared/impact/do/November2019"	// Path to processed data
global results "$path/results"		
global inputPath "$path/datasets"
cd "$results"									// Change to directory to store results
set more off
set trace off
*Loading processed data
	use $inputPath/impactDatabase_wide.dta, clear		

*Merge the asesor data here. 															
merge 1:1 idBaseSurvey using $inputPath/asesor2008.dta
drop _merge 

*===============================================================================
* Section 2 - Tables 
*===============================================================================
* Section 2.1 - Table on Short Term Borrowing
*-------------------------------------------------------------------------------
* 2.1.1.Calculates table content -----------------------------------------------
*-------------------------------------------------------------------------------
//initializes counter	
	local i = 9 								
	eststo clear
	
//iterate over outcome variables
foreach y in 											/// 
w5_shortTermBorrowing w5_stBorrowFamily w5_stBorrowProviders w5_stBorrowInstitutions		/// 9 10 11 12
w1_shortTermBorrowing w1_stBorrowFamily w1_stBorrowProviders w1_stBorrowInstitutions		/// 13 14 15 16
shortTermBorrowing stBorrowFamily stBorrowProviders stBorrowInstitutions			/// 17 18 19 20
IHSshortTermBorrowing IHSstBorrowFamily IHSstBorrowProviders IHSstBorrowInstitutions		/// 21 22 23 24
{

	// Marking subjects non-missing in both periods
        gen nonmiss = 1 if `y'2009 !=. & `y'2008 !=.

		reghdfe `y'2009 i.accountAccess `y'2008 if nonmiss == 1, abs(asesor2008) ///
			vce(cluster groupId) 
			
	//marking observations used in the regression 	
	gen `y'_s = e(sample) 
			
	// Adding control mean
	sum `y'2009 if accountAccess == 0
	if (abs(`r(mean)') < 100) {
		estadd local control_mean = trim("`: display %12.3fc r(mean)'")
	} 
	else {
		estadd local control_mean = trim("`: display %12.0fc r(mean)'")
	}
	
	drop nonmiss
 	estadd local ind_fixeff = "No"
	estadd local str_fixeff = "Yes"
	eststo A`i'
	local i = `i' + 1 //updates counter
}

//create variables to add as restrictions in the next regression 		
gen prShortTermBorrowingTot_s = w5_shortTermBorrowing_s
gen stBorrowTotalIndex_s = w1_shortTermBorrowing_s
gen prShortTermBorrowFamily_s = w5_stBorrowFamily_s
gen stBorrowFamilyIndex_s = w1_stBorrowFamily_s
gen prShortTermBorrowProv_s = w5_stBorrowProviders_s
gen stBorrowProvidersIndex_s = w1_stBorrowProviders_s
gen prShortTermBorrowInst_s = w5_stBorrowInstitutions_s
gen stBorrowInstitutionsIndex_s = w1_stBorrowInstitutions_s

//initializes counter	
	local i = 1 								
	
//iterate over outcome variables
foreach y in 											///
prShortTermBorrowingTot prShortTermBorrowFamily prShortTermBorrowProv prShortTermBorrowInst	/// 1 2 3 4 		
stBorrowTotalIndex stBorrowFamilyIndex stBorrowProvidersIndex stBorrowInstitutionsIndex		/// 5 6 7 8   
{

	// Marking subjects non-missing in both periods
        gen nonmiss = 1 if `y'2009 !=. & `y'2008 !=.

		reghdfe `y'2009 i.accountAccess `y'2008 if nonmiss == 1 & `y'_s == 1,	/// 
		abs(asesor2008) vce(cluster groupId) 
			
	// Adding control mean
	sum `y'2009 if accountAccess == 0
	if (abs(`r(mean)') < 100) {
		estadd local control_mean = trim("`: display %12.3fc r(mean)'")
	} 
	else {
		estadd local control_mean = trim("`: display %12.0fc r(mean)'")
	}
	
	drop nonmiss
 	estadd local ind_fixeff = "No"
	estadd local str_fixeff = "Yes"
	eststo A`i'
	local i = `i' + 1 //updates counter
}
	
*-------------------------------------------------------------------------------
* 2.1.2 Writes table -----------------------------------------------------------
*-------------------------------------------------------------------------------
//probability panel
	esttab A1 A2 A3 A4 using tab_borrow_ancova.tex, replace				/// 	
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber	///
	stats( control_mean, fmt(%12.3fc %12.0fc %12.0fc)				///
	label("Control mean")) mlabels(,none) 						///	
	collabels(, none) eqlabels(, none)  						///
	keep(1.accountAccess )								///
	varlabels(1.accountAccess "Account") 						///
	starlevels(* 0.1 ** 0.05 *** 0.01) 						///
	prehead(\begin{tabular}{l*{@M}{c}}  \hline \hline 				///
	& (1) 		& (2) 	     & (3) 	    & (4) 	\\ 			///
	& Total		& Owed to    & Owed to   & Owed to	\\ 			///
	& short-term	& family and & service   & business 	\\ 			///
	& borrowing	& friends    & providers & contacts and \\ 			///
	& 		&    	     & 	    & institutions	\\) 			///
	posthead(\hline \\ \multicolumn{@span}{l}{\textbf{Panel A:} Probability of Any Borrowing} \\ ) ///
	prefoot( ) postfoot( )  	
//Categories panel
	esttab A5 A6 A7 A8 using tab_borrow_ancova.tex, append 				///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber 	///
	stats( control_mean, fmt(%12.3fc %12.0fc %12.0fc)				///
	label("Control mean")) mlabels(,none) 						///
	collabels(, none) eqlabels(, none) 						/// 
	keep(1.accountAccess ) 	 							///
	varlabels(1.accountAccess "Account") 						///
	starlevels(* 0.1 ** 0.05 *** 0.01) 						///
	prehead( ) 									///
	posthead(\\ \multicolumn{@span}{l}{\textbf{Panel B:} Categories of Borrowing} \\) ///
	prefoot( ) postfoot( ) 
//winsor 5%
	esttab A9 A10 A11 A12 using tab_borrow_ancova.tex, append			///	
	cells(b(star fmt(%12.0fc)) se(par fmt(%12.0fc))) label style(tex) nonumber	///
	stats( control_mean, fmt(%12.0fc %12.0fc %12.0fc) 				///
	label("Control mean")) mlabels(,none)	 					///
	collabels(, none) eqlabels(, none) 						/// 
	keep(1.accountAccess ) 	 							///
	varlabels(1.accountAccess "Account") 						///
	starlevels(* 0.1 ** 0.05 *** 0.01) 						///
	prehead( ) 									///
	posthead(\\ \multicolumn{@span}{l}{\textbf{Panel C:} Amounts (Winsorized at the Top 5\%)} \\) ///
	prefoot( ) postfoot( ) 
//winsor 1%
	esttab A13 A14 A15 A16 using tab_borrow_ancova.tex, append			///	
	cells(b(star fmt(%12.0fc)) se(par fmt(%12.0fc))) label style(tex) nonumber	///
	stats( control_mean, fmt(%12.0fc %12.0fc %12.0fc) 				///
	label("Control mean")) mlabels(,none)	 					///
	collabels(, none) eqlabels(, none) 						/// 
	keep(1.accountAccess )	  							///
	varlabels(1.accountAccess "Account") 						///
	starlevels(* 0.1 ** 0.05 *** 0.01) 						///
	prehead( ) 									///
	posthead(\\ \multicolumn{@span}{l}{\textbf{Panel D:} Amounts (Winsorized at the Top 1\%)} \\) ///
	prefoot( ) postfoot( )
//non-winsorized 
	esttab A17 A18 A19 A20 using tab_borrow_ancova.tex, append			///	
	cells(b(star fmt(%12.0fc)) se(par fmt(%12.0fc))) label style(tex) nonumber	///
	stats( control_mean, fmt(%12.0fc %12.0fc %12.0fc) 				///
	label("Control mean" "Observations")) mlabels(,none)	 			///
	collabels(, none) eqlabels(, none) 						/// 
	keep(1.accountAccess )  							///
	varlabels(1.accountAccess "Account") 						///
	starlevels(* 0.1 ** 0.05 *** 0.01) 						///
	prehead( ) 									///
	posthead(\\ \multicolumn{@span}{l}{\textbf{Panel E:} Amounts (Non-Winsorized)} \\) ///
	prefoot( ) postfoot( )
//IHS
	esttab A21 A22 A23 A24 using tab_borrow_ancova.tex, append 			///
	cells(b(star fmt(%12.3fc)) se(par fmt(%12.3fc))) label style(tex) nonumber 	///
	stats( control_mean N ind_fixeff str_fixeff, fmt(%12.0fc %12.0fc %12.0fc) 	///
	label("Control mean" "\\ \hline Observations" "Individual FE" "Stratum FE")) mlabels(,none)	///
	collabels(, none) eqlabels(, none) 						/// 
	keep(1.accountAccess )  							///
	varlabels(1.accountAccess "Account") 						///
	starlevels(* 0.1 ** 0.05 *** 0.01) 						///
	prehead( ) 									///
	posthead(\\ \multicolumn{@span}{l}{\textbf{Panel F:} Inverse Hyperbolic Sine of Amount} \\) ///	
	prefoot( ) postfoot(\hline \hline \end{tabular})  
